package com.heima.wemedia.service.impl;

import com.alibaba.fastjson.JSON;
import com.heima.common.constants.CommentConstants;
import com.heima.common.constants.WemediaConstants;
import com.heima.common.exception.CustomException;
import com.heima.feign.article.IArticleClient;
import com.heima.feign.comment.ICommentClient;
import com.heima.feign.user.IUserClient;
import com.heima.model.comment.pojos.CommentLikeDto;
import com.heima.model.comment.pojos.CommentSaveDto;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojos.ApUser;
import com.heima.model.wemedia.dtos.*;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.utils.thread.WeMediaThreadLocalUnit;
import com.heima.wemedia.service.CommentService;
import com.heima.wemedia.service.GreenScanService;
import com.heima.wemedia.service.WmCommentService;
import com.heima.wemedia.service.WmUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.avro.data.Json;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;


@Service
@Slf4j
//@SuppressWarnings("all")
public class WmCommentServiceImpl implements WmCommentService {
    @Autowired
    private IArticleClient iArticleClient;
    @Autowired
    private KafkaTemplate<String,String> kafkaTemplate;
    @Autowired
    private ICommentClient commentClient;
    @Autowired
    private IUserClient userClient;
    @Autowired
    private WmUserService wmUserService;
    @Autowired
    private CommentService commentService;
    /**
     * 查询发布的文章
     *
     * @param dto
     * @return
     */
    @Override
    public PageResponseResult findNewsComments(WmCommentDto dto) {
        //1、参数校验
        if (dto == null || dto.getEndDate() == null || dto.getBeginDate() == null) {
            return (PageResponseResult) ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        dto.checkParam();
        //2、拿到当前登录自媒体账号id
        Integer userId = WeMediaThreadLocalUnit.get();
        //2.1、通过自媒体账户id，在文章微服务中查询信息
        PageResponseResult article = iArticleClient.getArticle(userId, dto);
        if (!article.getCode().equals(200)) {
            return (PageResponseResult) ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR,"WmCommentServiceImpl 36");
        }
        //3、直接返回参数
        return article;
    }

    /**
     * 打开或关闭评论
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult updateCommentStatus(WmUpdateCommentDto dto) {
        //1、参数校验
        if (dto == null || dto.getOperation() == null || dto.getArticleId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2、给kafka发送消息，异步处理
        kafkaTemplate.send(WemediaConstants.COMMENT_TOPIC,JSON.toJSONString(dto));
        //3、若是关闭评论，则删除所有评论 kafka发送消息到comment微服务
        kafkaTemplate.send(CommentConstants.DEL_COMMENT,JSON.toJSONString(dto.getArticleId()));
        //4、返回成功消息
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 评论详情
     * @param dto
     * @return
     */
    @Override
    public ResponseResult commentList(CommentListDto dto) {
        //1、参数校验
        if (dto == null || dto.getArticleId() == null) {
            return  PageResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2、远程调用评论微服务
        return commentClient.list(dto);
    }

    /**
     * 作者点赞评论
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult like(CommentLikeDto dto) {
        //1、参数校验
        if (dto == null || dto.getOperation() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2、业务处理
        //2.1、获取当前自媒体id
        ApUser apUser = getUser();
        //封装传输对象
        AuthorLikeDto authorLikeDto = new AuthorLikeDto();
        authorLikeDto.setUserId(apUser.getId());
        authorLikeDto.setCommentId(dto.getCommentId());
        authorLikeDto.setOperation(dto.getOperation());
        ResponseResult responseResult = commentClient.like(authorLikeDto);
        if (!responseResult.getCode().equals(200)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR,"107 error");
        }
        return responseResult;
    }

    /**
     * 作者回复评论
     * @param dto
     * @return
     */
    @Override
    public ResponseResult repay(WmRepayCommentDto dto) {
        //1、参数校验
        if (dto == null || dto.getCommentId() == null || StringUtils.isEmpty(dto.getContent())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2、业务处理
        //2.0、得到当前登录自媒体id
        ApUser apUser = getUser();
        dto.setUserId(apUser.getId());
        dto.setAuthorName(apUser.getName());
        //2.1、审核评论回复是否敏感违规
        CommentSaveDto commentSaveDto = new CommentSaveDto();
        commentSaveDto.setContent(dto.getContent());
        ResponseResult responseResult = commentService.checkComment(commentSaveDto);
        if (!responseResult.getCode().equals(200)) {
            //返回不是200 就是各种违规敏感，直接返回
            return responseResult;
        }
        //2.2、审核文字通过 远程调用评论微服务
        return commentClient.repay(dto);
    }

    /**
     * 得到当前自媒体id对应的用户信息
     * @return
     */
    private ApUser getUser(){
        Integer userId = WeMediaThreadLocalUnit.get();
        WmUser wmuser = wmUserService.getById(userId);
        //2.2、通过自媒体id得到登录用户id
        ResponseResult useInfo = userClient.getUseInfo(wmuser.getApUserId());
        if (!useInfo.getCode().equals(200)) {
            throw new CustomException(AppHttpCodeEnum.PARAM_INVALID);
        }
        //得到用户信息
        return JSON.parseObject(JSON.toJSONString(useInfo.getData()), ApUser.class);
    }
}
